<template>
  <div class="middle-content">
    <!-- 文章列表 -->
    <ArticleList :articleList="articleList" />
    <!-- 分页 -->
    <el-pagination
      background
      @current-change="handleCurrentChange"
      layout="prev, pager, next"
      class="pagination"
      :total="pagination.total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getArticleList } from '@/api/article'
export default {
  name: 'MiddleContent',
  data() {
    return {
      articleList: [],
      pagination: {
        pageSize: 10,
        pageNumber: 1,
        total: 0,
        totalPages: 0
      }
    }
  },
  props: {
    categoryId: {
      type: String,
      default: ''
    }
  },
  watch: {
    categoryId() {
      this.pagination.pageNumber = 1
      this.getUserArticleList()
    }
  },
  created() {
    this.getUserArticleList()
  },
  methods: {
    // 切换页码
    handleCurrentChange(val) {
      // 页码加一然后重新获取文章列表
      this.pagination.pageNumber = val
      this.getUserArticleList()
    },

    // 获取文章列表
    async getUserArticleList() {
      const params = {
        categoryId: this.categoryId || '',
        pageNumber: this.pagination.pageNumber,
        pageSize: this.pagination.pageSize
      }
      try {
        const res = await getArticleList(params)
        if (res.code === 200) {
          this.articleList = res.data.list
          this.pagination.pageNumber = res.data.pagination.pageNumber
          this.pagination.total = res.data.pagination.total
          this.pagination.totalPages = res.data.pagination.totalPages
        }
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.middle-content {
  margin: 0 20px 20px 20px;
  background-color: #fff;
  .list-item {
    padding: 20px;
    color: #4b587c;
    border-bottom: 1px solid #ccc;
    &:last-child {
      border-bottom: none;
    }
  }
  .title {
    font-size: 16px;
    font-weight: 600;
    line-height: 20px;
    color: black;
  }
  .content {
    margin-top: 16px;
    font-size: 13px;
  }
  .bottom-box {
    margin-top: 20px;
    p {
      margin-right: 20px;
    }
  }
  .pagination {
    padding: 20px 0 20px 20px;
  }
}
</style>
